import XCTest
@testable import YXSilver

final class YXSilverTests: XCTestCase {
    
    func testYXURLEncode() {
        let text = "快手助力"
        XCTAssertEqual("%E5%BF%AB%E6%89%8B%E5%8A%A9%E5%8A%9B", text.yx_URLEncoding())
    }
    
    func testYXUIColor() {
        // RGB
        let white = UIColor(0xFFFFFF)
        XCTAssertEqual(white.yx_rgb, "#FFFFFF")
        XCTAssertEqual(white.yx_rgba, "#FFFFFFFF")
        
        // RGBA
        let black = UIColor("#000000FF")
        XCTAssertEqual(black.yx_rgb, "#000000")
        XCTAssertEqual(black.yx_rgba, "#000000FF")
    }
    
    func testYXDecimalNumber() {
        let number:Double = 21.999
        let round = number.yx_decimal.yx_rounding(scale: 2)
        XCTAssertEqual(round, "21.99")
    }

    func testYXDate_startDayOfThisWeek() {
        
        // 2021-02
        let d21: TimeInterval = 1613836800
        let d22: TimeInterval = 1613923200
        let d23: TimeInterval = 1614009600
        let d24: TimeInterval = 1614096000
        let d25: TimeInterval = 1614182400
        let d26: TimeInterval = 1614268800
        let d27: TimeInterval = 1614355200
        let d28: TimeInterval = 1614441600
        let d29: TimeInterval = 1614528000
        
        let sd21 = Date.yx_startDayOfThisWeek(d21.yx_date)
        let sd22 = Date.yx_startDayOfThisWeek(d22.yx_date)
        let sd23 = Date.yx_startDayOfThisWeek(d23.yx_date)
        let sd24 = Date.yx_startDayOfThisWeek(d24.yx_date)
        let sd25 = Date.yx_startDayOfThisWeek(d25.yx_date)
        let sd26 = Date.yx_startDayOfThisWeek(d26.yx_date)
        let sd27 = Date.yx_startDayOfThisWeek(d27.yx_date)
        let sd28 = Date.yx_startDayOfThisWeek(d28.yx_date)
        let sd29 = Date.yx_startDayOfThisWeek(d29.yx_date)
        
        XCTAssertEqual(sd22.yx_unix, d22)
        
        XCTAssertEqual(sd22, sd23)
        XCTAssertEqual(sd22, sd24)
        XCTAssertEqual(sd22, sd25)
        XCTAssertEqual(sd22, sd26)
        XCTAssertEqual(sd22, sd27)
        XCTAssertEqual(sd22, sd28)
        
        XCTAssertNotEqual(sd21, sd22)
        XCTAssertNotEqual(sd21, sd29)
        XCTAssertNotEqual(sd22, sd29)
    }
    
    func testYXDate_startDayOfThisMonth() {
        
        // 2021-02
        let m2d1: TimeInterval = 1612108800
        let m2d28: TimeInterval = 1614441600
        let m3d1: TimeInterval = 1614528000
        let m3d31: TimeInterval = 1617120000
        let m4d1: TimeInterval = 1617206400
        let m4d30: TimeInterval = 1619712000
        
        let sm21 = Date.yx_startDayOfThisMonth(m2d1.yx_date)
        let sm228 = Date.yx_startDayOfThisMonth(m2d28.yx_date)
        let sm31 = Date.yx_startDayOfThisMonth(m3d1.yx_date)
        let sm331 = Date.yx_startDayOfThisMonth(m3d31.yx_date)
        let sm41 = Date.yx_startDayOfThisMonth(m4d1.yx_date)
        let sm430 = Date.yx_startDayOfThisMonth(m4d30.yx_date)
        
        XCTAssertEqual(sm21.yx_unix, m2d1)
        XCTAssertEqual(sm31.yx_unix, m3d1)
        XCTAssertEqual(sm41.yx_unix, m4d1)
        
        XCTAssertEqual(sm21.yx_unix, sm228.yx_unix)
        XCTAssertEqual(sm31.yx_unix, sm331.yx_unix)
        XCTAssertEqual(sm41.yx_unix, sm430.yx_unix)
    }
}
